/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = rtp_report.c                                       */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/17 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../src/module/rtp/config.h"
#include "../test_rtp_comm.h"
#include <rtp/rtp_core.h>
#include <rtp/rtp_header.h>
#include <rtp/rtp_comm.h>
#include <apmtest_comm.h>
#include <frreport_opt.h>
#include <frvar_field.h>

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    Frlist_node *p_st_node = NULL;
    Frvar_field *p_st_field = NULL;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_rtp_create_ct(&p_st_conn, TEST_RTP_SRC_IP, TEST_RTP_DST_IP, TEST_RTP_SRC_PORT, TEST_RTP_DST_PORT), FR_SUCC);

    p_st_conn->flg_has_master = FR_YES;
    p_st_conn->st_masterInfo.exp_src_ip = iptools_parse_ipaddr(TEST_RTP_SRC_IP);
    p_st_conn->st_masterInfo.exp_src_port = TEST_RTP_SRC_PORT;
    p_st_conn->st_masterInfo.exp_dst_ip = iptools_parse_ipaddr(TEST_RTP_DST_IP);
    p_st_conn->st_masterInfo.exp_dst_port = TEST_RTP_DST_PORT;

    unsigned long ul_invite_begin_timestamp;
    struct timeval st_timestamp_first;
    st_timestamp_first.tv_sec = 1454549154L;
    st_timestamp_first.tv_usec = 112233L;
    ul_invite_begin_timestamp = rtp_get_timestamp(&st_timestamp_first);
    char arr_invite_time[32] = {'\0'};
    snprintf(arr_invite_time, sizeof(arr_invite_time), "%lu", ul_invite_begin_timestamp);
    p_st_conn->st_masterInfo.p_lst_hdr_param = frlist_create();
    p_st_field = frvarfield_str(APM_SIP_INVITE_BEGIN_TIME, arr_invite_time);
    p_st_node = frlist_node_create(p_st_field);
    frlist_push(p_st_conn->st_masterInfo.p_lst_hdr_param, p_st_node);

    p_st_field = frvarfield_str(APM_SIP_INVITE_PLAY_URI, "sip:42010606001320082255_1@10.72.1.125");
    p_st_node = frlist_node_create(p_st_field);
    frlist_push(p_st_conn->st_masterInfo.p_lst_hdr_param, p_st_node);

    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    unsigned long ul_fbt = 1454549354123456L;
    unsigned long ul_bytes = 4249L;
    unsigned long ul_ffft = 1454549354123456L;
    unsigned long ul_pluff = 0;
    int i_analyze_ret = 0;

    rtp_report(p_st_conn, &st_hash_report, ul_fbt, ul_bytes, ul_ffft, ul_pluff, &i_analyze_ret);

    // 测试结果
    Frlist_header *p_list_report = NULL;
    Frlist_node *p_st_node_report = NULL;
    Frreport_data *p_st_report_data = NULL;
    p_list_report = &((&st_hash_report)->p_list_array[0]);

    p_st_node_report = FRLIST_FIRST_NODE(p_list_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_node_report);

    p_st_report_data = (Frreport_data *) FRLIST_GET_NODE_DATA(p_st_node_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_report_data);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_SESSION_ID, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, TEST_RTP_SESSION_ID);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_REQ_ISRESPREQUIRED, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, FR_YES);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_REQ_START_TIMESTAMP, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1454549154112L);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_REQ_END_TIMESTAMP, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1454549154112L);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_REQ_SEND_TIME_US, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 0);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_REQ_DATA_BYTES, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 0);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_TRANS_TITLE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "PLAY sip:42010606001320082255_1@10.72.1.125");

    p_st_node_report = FRLIST_NEXT_NODE(p_st_node_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_node_report);

    p_st_report_data = (Frreport_data *) FRLIST_GET_NODE_DATA(p_st_node_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_report_data);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_START_TIMESTAMP, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1454549354123L);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_END_TIMESTAMP, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1454549354123L);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_SERVER_TIME_US, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 200011223L);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_DOWNLOAD_TIME_US, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 0);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_DATA_BYTES, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 4249L);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_RTP_REPORT_RESP_PACKETS_LOST, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 0);

    frhash2_free(&st_hash_report, list_report_node_free);

    test_rtp_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite_rtp_report(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" rtp_report()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
